home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / motif / oedgeflag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.7 KB  |  335 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * oedgeflag.c : openGL (motif) example showing use of edge flag.
  20.  *
  21.  * Author : Yusuf Attarwala
  22.  *          SGI - Applications
  23.  * Date   : Sep 93
  24.  *
  25.  *    press  left   button to move object 
  26.  *           middle button to turn off edge flaging
  27.  *           right  button to turn on  edge flaging
  28.  *
  29.  *
  30.  *---------------------------------------------------------------------------*/
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <strings.h>
  34.  
  35. #include <Xm/Xm.h> 
  36. #include <Xm/Frame.h>               /* for frame widgets */
  37. #include <Xm/Form.h>               /* for frame widgets */
  38. #include <X11/keysym.h>             /* keyboard translations */
  39. #include <X11/StringDefs.h>
  40.  
  41. #include <GL/gl.h>                  /* gl includes */
  42. #include <GL/glu.h>                 /* utility library includes */
  43. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  44.  
  45. /* function declarations */
  46.  
  47. void 
  48.     createToplevel(void),
  49.     drawScene(void),
  50.     setMatrix(void),
  51.     animation(void),
  52.     exposeCB(Widget,XtPointer,XtPointer),
  53.     resizeCB(Widget,XtPointer,XtPointer),
  54.     initCB(Widget,XtPointer,XtPointer),
  55.     inputCB(Widget,XtPointer,XtPointer);
  56.  
  57.  
  58. /* global variables */
  59.             
  60. Display       *display;             /* current display */
  61. XtAppContext  appContext;           /* X application context */
  62. float         ax,ay,az;             /* angles for animation */
  63.  
  64. Widget        toplevel,       /* toplevel shell */
  65.               glw;            /* current widget */
  66.              
  67. GLXContext    glxc;           /* current glx context */
  68.  
  69. /* coordinates of star */
  70. static GLfloat   starv[10][3] = {3.5,5.0,0.0,
  71.                  2.0,2.0,0.0,
  72.                  5.0,4.0,0.0,
  73.                  8.0,2.0,0.0,
  74.                  6.5,5.0,0.0,
  75.                  8.0,6.0,0.0,
  76.                  6.0,6.0,0.0,
  77.                  5.0,8.0,0.0,
  78.                  4.0,6.0,0.0,
  79.                  2.0,6.0,0.0};
  80.  
  81. #define NTRI 8   /* number of triangles in the star */
  82.  
  83. /* connectivity data */
  84. static int       stari[NTRI][3] = {0,1,2,
  85.                    2,3,4,
  86.                    4,5,6,
  87.                    6,7,8,
  88.                    8,9,0,
  89.                    0,2,8,
  90.                    8,2,6,
  91.                    6,2,4};
  92.  
  93. static GLfloat starcolor[NTRI][3] = {1.0,0.0,0.0,
  94.                      0.0,1.0,0.0,
  95.                      0.0,0.0,1.0,
  96.                      1.0,0.0,1.0,
  97.                      0.0,1.0,1.0,
  98.                      1.0,1.0,0.0,
  99.                      1.0,1.0,1.0,
  100.                      1.0,0.5,0.5};
  101.  
  102.  
  103. int doEdgeFlag = 0;
  104.  
  105. /* edge flags */
  106. static GLboolean stare[NTRI][3] = {TRUE,TRUE,FALSE,
  107.                                    TRUE,TRUE,FALSE,
  108.                                    TRUE,TRUE,FALSE,
  109.                                    TRUE,TRUE,FALSE,
  110.                                    TRUE,TRUE,FALSE,
  111.                                    FALSE,FALSE,FALSE,
  112.                                    FALSE,FALSE,FALSE,
  113.                                    FALSE,FALSE,FALSE};
  114.  
  115. Arg args[20];
  116. int acnt;
  117.  
  118. void 
  119. main(int argc, char** argv)
  120. {
  121.     XtToolkitInitialize();
  122.     appContext = XtCreateApplicationContext();
  123.     display    = XtOpenDisplay(appContext, NULL, "Oedge","oedge",NULL,0,
  124.                               &argc,argv);
  125.     if (!display) {
  126.         printf("%s : Unable to open display\n",argv[0]);
  127.         exit(0);
  128.     }
  129.  
  130.     printf("\n---------------------------------------------\n");
  131.     printf("OpenGL edge flag example \n\n");
  132.     printf("Press:  left   button for animation\n\
  133.         middle button to turn OFF edgeflag (default)\n\
  134.         right  button to turn ON  edgeflag \n");
  135.  
  136.     createToplevel();             /* create widget hierarchy */
  137.     XtAppMainLoop(appContext);    /* loop forever */
  138. }
  139.  
  140.  
  141. void
  142. createToplevel(void)
  143. {
  144.     Widget frame;
  145.  
  146.     acnt = 0;
  147.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  148.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  149.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  150.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  151.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  152.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  153.     toplevel  = XtAppCreateShell("openGL edge","xtext",
  154.                                   applicationShellWidgetClass,
  155.                                   display,args,acnt);
  156.  
  157.     /* create a frame to hold glx widget */
  158.     frame   = XtVaCreateManagedWidget("frame", 
  159.                   xmFrameWidgetClass, toplevel, 
  160.                   NULL);
  161.  
  162.     /* create a double buffer widget, in rgb mode and manage it */
  163.     acnt = 0;
  164.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  165.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  166.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  167.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  168.     XtManageChild(glw);
  169.  
  170.     /* register callbacks */
  171.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  172.  
  173.     /* realize widget hierarchy */
  174.     XtRealizeWidget(toplevel);
  175.  
  176.     /* additional callbacks */
  177.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  178.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  179.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  180.  
  181. }
  182.  
  183. void
  184. drawScene(void)
  185. {
  186.     int i;
  187.     glClearColor(0.0, 0.0, 0.0, 0.0);
  188.     glClear(GL_COLOR_BUFFER_BIT);
  189.  
  190.     glPushMatrix();
  191.  
  192.     glTranslatef(4.0,4.0,0.0);
  193.     glRotatef (ax,1.0,0.0,0.0);       /* animation angles */
  194.     glRotatef (-ay,0.0, 1.0, 0.0);
  195.     glRotatef (az,0.0, 0.0, 1.0);
  196.     glTranslatef(-4.0,-4.0,0.0);
  197.  
  198.     /* render the 8 triangles */
  199.     for (i=0;i<NTRI;i++) {
  200.         glColor3fv(starcolor[i]);
  201.         if (doEdgeFlag) {
  202.         glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  203.         glBegin(GL_POLYGON);
  204.         }
  205.         else {
  206.             glBegin(GL_LINE_LOOP);
  207.         }
  208.     if (doEdgeFlag) glEdgeFlag(stare[i][0]);
  209.     glVertex3fv(starv[stari[i][0]]);
  210.     if (doEdgeFlag) glEdgeFlag(stare[i][1]);
  211.     glVertex3fv(starv[stari[i][1]]);
  212.     if (doEdgeFlag) glEdgeFlag(stare[i][2]);
  213.     glVertex3fv(starv[stari[i][2]]);
  214.     glEnd();
  215.     }
  216.  
  217.     glPopMatrix();
  218.  
  219.     glFlush();
  220.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  221.  
  222. }
  223.  
  224. void
  225. setMatrix(void)
  226. {
  227.     glMatrixMode(GL_PROJECTION);
  228.     glLoadIdentity();
  229.     glOrtho(-0.5,9.5,-0.5,9.5,-10.0,10.0);
  230.     glMatrixMode(GL_MODELVIEW);
  231.     glLoadIdentity();
  232. }
  233.  
  234. void
  235. animation(void)
  236. {
  237.     register int i;
  238.     /* animate the star */
  239.  
  240.     for (i=0;i<60;i++) {
  241.     /*
  242.         ax += 5.0;
  243.         ay -= 2.0;
  244.     */
  245.     az += 3.0;
  246.         if (ax >= 360)  ax = 0.0;
  247.         if (ay <= -360) ay = 0.0;
  248.         if (az >= 360)  az = 0.0;
  249.         drawScene();
  250.     }
  251. }
  252.  
  253. void 
  254. inputCB(Widget w, XtPointer client_data, XtPointer call)
  255. {
  256.     char            string[31];
  257.     XComposeStatus  composeStatus;
  258.     KeySym keysym;
  259.     GLwDrawingAreaCallbackStruct *call_data;
  260.  
  261.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  262.  
  263.     switch(call_data->event->type) {
  264.     case ButtonPress:
  265.         switch (call_data->event->xbutton.button) {
  266.         case Button1:
  267.         animation();
  268.             break;
  269.         case Button2 :
  270.         doEdgeFlag = 0;
  271.         XtVaSetValues(toplevel,XmNtitle, "Edge Flag OFF",NULL);
  272.         drawScene();
  273.             break;
  274.         case Button3 :
  275.         doEdgeFlag = 1;
  276.         XtVaSetValues(toplevel,XmNtitle, "Edge Flag ON",NULL);
  277.         drawScene();
  278.             break;
  279.         }
  280.         break;
  281.     case KeyPress :
  282.         XLookupString((XKeyEvent *)call_data->event,string,
  283.                       30,&keysym,&composeStatus);
  284.         switch(keysym) {
  285.         case XK_Escape :
  286.             exit(0);
  287.             break;
  288.         }
  289.     break;
  290.     default:
  291.         break;
  292.     }
  293. }
  294.  
  295. void 
  296. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  297. {
  298.     GLwDrawingAreaCallbackStruct *call_data;
  299.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  300.  
  301.  
  302.     GLwDrawingAreaMakeCurrent(w, glxc);
  303.     glViewport(0, 0, call_data->width, call_data->height);
  304.     setMatrix();
  305.     drawScene();
  306. }
  307.  
  308. void 
  309. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  310. {
  311.     GLwDrawingAreaCallbackStruct *call_data;
  312.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  313.  
  314.  
  315.     GLwDrawingAreaMakeCurrent(w, glxc);
  316.     glViewport(0, 0, call_data->width, call_data->height);
  317.     drawScene();
  318. }
  319.  
  320. void
  321. initCB(Widget w, XtPointer client_data, XtPointer call)
  322. {
  323.     XVisualInfo *vi;
  324.  
  325.  
  326.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  327.     XtGetValues(w, args, 1);
  328.  
  329.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  330.  
  331.     ax = 10.0;
  332.     ay = -10.0;
  333.     az = 0.0;
  334. }
  335.